package org.jeecg.modules.demo.weixin.merchantpay.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.api.UpmsHelloApi;
import org.jeecg.modules.demo.weixin.entity.MerchantPay;
import org.jeecg.modules.demo.weixin.merchantpay.service.IMerchantPayService;
import org.jeecg.modules.demo.weixin.smtprofitsharingreceiver.service.ISmtProfitsharingReceiverService;
import org.jeecg.modules.demo.weixin.smtsharingmember.service.ISmtSharingMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 商家支付管理
 * @Author: jeecg-boot
 * @Date: 2023-11-04
 * @Version: V1.0
 */
@Api(tags = "商家支付管理")
@RestController
@RequestMapping("/weixin/merchantpay/merchantPay")
@Slf4j
public class MerchantPayController extends JeecgController<MerchantPay, IMerchantPayService> {
    @Autowired
    private IMerchantPayService merchantPayService;

    @Autowired
    private UpmsHelloApi upmsHelloApi;

    @Autowired
    private ISmtProfitsharingReceiverService smtProfitsharingReceiverService;

    @Autowired
    private ISmtSharingMemberService smtSharingMemberService;
    /**
     * 分页列表查询
     *
     * @param merchantPay
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "商家支付管理-分页列表查询")
    @ApiOperation(value = "商家支付管理-分页列表查询", notes = "商家支付管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<MerchantPay>> queryPageList(MerchantPay merchantPay,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<MerchantPay> queryWrapper = QueryGenerator.initQueryWrapper(merchantPay, req.getParameterMap());
        Page<MerchantPay> page = new Page<MerchantPay>(pageNo, pageSize);
        IPage<MerchantPay> pageList = merchantPayService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param merchantPay
     * @return
     */
//    @AutoLog(value = "商家支付管理-添加")
    @ApiOperation(value = "商家支付管理-添加", notes = "商家支付管理-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody MerchantPay merchantPay) {
        merchantPayService.save(merchantPay);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param merchantPay
     * @return
     */
//    @AutoLog(value = "商家支付管理-编辑")
    @ApiOperation(value = "商家支付管理-编辑", notes = "商家支付管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody MerchantPay merchantPay) {
        merchantPayService.updateById(merchantPay);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
//    @AutoLog(value = "商家支付管理-通过id删除")
    @ApiOperation(value = "商家支付管理-通过id删除", notes = "商家支付管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        merchantPayService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
//    @AutoLog(value = "商家支付管理-批量删除")
    @ApiOperation(value = "商家支付管理-批量删除", notes = "商家支付管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.merchantPayService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "商家支付管理-通过id查询")
    @ApiOperation(value = "商家支付管理-通过id查询", notes = "商家支付管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<MerchantPay> queryById(@RequestParam(name = "id", required = true) String id) {
        MerchantPay merchantPay = merchantPayService.getById(id);

        if (merchantPay == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(merchantPay);
    }


    /**
     * 通过id查询
     *
     * @param merId
     * @return
     */
    //@AutoLog(value = "商家支付管理-通过id查询")
    @ApiOperation(value = "商家支付管理-通过id查询", notes = "商家支付管理-通过id查询")
    @GetMapping(value = "/queryMerId")
    public Result<MerchantPay> queryMerId(@RequestParam(name = "merId", required = true) String merId) {
        LambdaQueryWrapper<MerchantPay> merchantPayLambdaQueryWrapper = new LambdaQueryWrapper<>();
        merchantPayLambdaQueryWrapper.eq(MerchantPay::getMerchantId, Integer.valueOf(merId));
        MerchantPay  one  = merchantPayService.getOne(merchantPayLambdaQueryWrapper);
        if (one == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(one);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param merchantPay
     */
    @RequiresPermissions("merchantpay:merchant_pay:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MerchantPay merchantPay) {
        return super.exportXls(request, merchantPay, MerchantPay.class, "商家支付管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("merchantpay:merchant_pay:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MerchantPay.class);
    }


    /**
     * 修改
     *
     * @param merchantPay
     * @return
     */
    @PostMapping(value = "/updateSpecial")
    public void updateSpecial(@RequestBody MerchantPay merchantPay) {
        UpdateWrapper<MerchantPay> payUpdateWrapper = new UpdateWrapper<>();
        payUpdateWrapper.set("special_sub_merchants", merchantPay.getSpecialSubMerchants());
        payUpdateWrapper.eq("merchant_id", merchantPay.getMerchantId());
        merchantPayService.update(payUpdateWrapper);
    }


    /**
     * 查询商家信息是否填写
     *
     * @param merId
     */
    @GetMapping(value = "/querySpecial")
    MerchantPay querySpecial(@RequestParam String merId) {
        log.info("请求参数是："+merId);
        LambdaQueryWrapper<MerchantPay> merchantPayLambdaQueryWrapper = new LambdaQueryWrapper<>();
        merchantPayLambdaQueryWrapper.eq(MerchantPay::getMerchantId, Integer.valueOf(merId));
        MerchantPay  one  = merchantPayService.getOne(merchantPayLambdaQueryWrapper);
        return  one;
    }





}
